<?php
/**
 * Created by PhpStorm.
 * User: hongy
 * Date: 2016/2/4
 * Time: 17:59
 */

namespace Home\Controller;


use Common\Common\wechat;
use Think\Controller;

class RedpackController extends Controller
{
    public function index(){

        if($_SESSION['him']==''||$_SESSION['openid']=='') {
            Header("Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2e1ad7100fd503cc&redirect_uri=http://tubutravel.com/index.php/Home/Redpack/first&response_type=code&scope=snsapi_base&state=123#wechat_redirect");
        }else{
            $friend = D('Redpack')->getFriend($_SESSION['openid']);
            if($friend){
                $data['ka'] = $friend['ka'];
                $data['money'] = $friend['money'];
            }else{
                $money =  rand(100,1200);
                $moneys = M('Total')->where(array('id'=>1))->find();
                if($moneys['money'] == 0){
                    $money = rand(301,1200);
                }elseif($moneys['money']<3){
                    $money = $moneys['money']*100;
                }
                if($money<=300) {
                    $data['status'] = 1;
                    $data['ka'] = 0;
                    $data['money'] = $money/100;
                    $moneys['money']-=$data['money']/100;
                    M('Total')->where(array('id'=>1))->data(array('money'=>$moneys['money']))->save();
                }elseif($money<=450){
                    $data['status'] = 1;
                    $data['ka'] = 1;
                    $data['money'] = $money/100;
                }elseif($money<=600){
                    $data['status'] = 1;
                    $data['ka'] = 2;
                    $data['money'] = $money/100;
                }elseif($money<=900){
                    $data['status'] = 1;
                    $data['ka'] = 3;
                    $data['money'] = $money/100;
                }else{
                    $data['status'] = 1;
                    $data['ka'] = 4;
                    $data['money'] = $money/100;
                }
                D('Redpack')->update($_SESSION['openid'],$_SESSION['him'],$data);
            }
            $this->assign('data',$data);
            $this->display();
        }

    }

    public function first(){
        $code = $_REQUEST['code'];
        $token = wechat::getAccessToken($code);
        $_SESSION['openid'] = $token->openid;
        $check = wechat::getInfo($token->openid);
        if($check->subscribe==0) $this->assign('flag','fail');
        $rpmodel = D('Redpack');
        $data = $rpmodel->checkRp($token->openid);
        if($data) {
            $this->redirect('Redpack/second');
        }
        else
        $this->display();
    }

    public function thrid(){
        $code = $_REQUEST['code'];
        $token = wechat::getAccessToken($code);
        $_SESSION['openid'] = $token->openid;
        $check = wechat::getInfo($token->openid);
        if($check->subscribe==0) $this->assign('flag','fail');
        $rpmodel = D('Redpack');
        $data = $rpmodel->checkRp($token->openid);
        if($data) {
            $this->redirect('Redpack/second');
        }
        else
            $this->display();
    }

    public function second(){
        if($_SESSION['openid']=='') {
            Header("Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2e1ad7100fd503cc&redirect_uri=http://tubutravel.com/index.php/Home/Redpack/first&response_type=code&scope=snsapi_base&state=123#wechat_redirect");
        }elseif(!isset($_POST['myname'])&&!(D('Redpack')->findMe($_SESSION['openid']))){
            Header("Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2e1ad7100fd503cc&redirect_uri=http://tubutravel.com/index.php/Home/Redpack/first&response_type=code&scope=snsapi_base&state=123#wechat_redirect");
        } else{
            $rpmodel = D('Redpack');
            $friend = $rpmodel->getFriend($_SESSION['openid']);
            if($friend){
                $_SESSION['him'] = $friend['openid'];
                $this->assign('zhfu',$friend['zhufuyu']);
                $data = $rpmodel->getRp($_SESSION['openid']);
                $this->assign('data',$data);
                $this->assign('flag','success');
                $this->display();
            }else{
                $data = $rpmodel->getRp($_SESSION['openid']);
                if(!$data){
                    $data = array(
                        'myname' => I('myname'),
                        'yourname' => I('yourname'),
                        'zhufuyu'   =>  I('zhufuyu'),
                        'openid'    =>  $_SESSION['openid'],
                    );
                    $rpmodel->addRp($data);
                }
                $this->assign('data',$data);
                $check = $rpmodel->checkPerson($data);
                $b=0;
                if(count($check)==0) {

                    $this->display();
                }else{
                    for($i = 0; $i<count($check); $i++){
                        if($check[$i]['friend']==$_SESSION['openid']){
                            $_SESSION['him'] = $check[$i]['openid'];
                            $this->assign('zhfu',$check[$i]['zhufuyu']);
                            $b=1;
                            break;
                        }
                    }

                    if($b==0){
                        for($i = 0; $i<count($check); $i++){
                            if($check[$i]['friend']==NULL){
                                $_SESSION['him'] = $check[$i]['openid'];
                                $this->assign('zhfu',$check[$i]['zhufuyu']);
                                $data1 = array(
                                    'friend' => $check[$i]['openid']
                                );
                                $data2 = array(
                                    'friend' => $data['openid']
                                );
                                $rpmodel->updateOpenid($data['openid'],$check[$i]['openid'],$data1,$data2);
                                break;
                            }
                        }
                    }
                    $this->display();
                }
            }


        }

    }
    public function deleteJC(){
        D('Redpack')->deleteJC();
        $this->ajaxReturn('删除成功');
    }
    public function deleteHY(){
        D('Redpack')->deleteHY();
        $this->ajaxReturn('删除成功');
    }

    public function loadOut(){
        $data = D('Redpack')->loadOut();
        for($i = 0;$i<count($data);$i++){
            echo $data[$i]['openid'].'<br/>';
        }
    }
}